﻿var map = null;      
var routeRequest;
var bingMapkey;
var routeIcon;
function getMap()      
{        
	map = new Microsoft.Maps.Map(
		document.getElementById('bingMap'),
		{ credentials: bingMapkey }
		);
	map.getCredentials(callRouteService);
}

function callRouteService(credentials)
{
	var mapscript = document.createElement('script');                
	mapscript.type = 'text/javascript';          
	mapscript.src = routeRequest + '&routePathOutput=Points&output=json&jsonp=routeCallback&key=' + credentials;
	document.getElementById('bingMap').appendChild(mapscript);   
}

function routeCallback(result)      
{        
	var output = document.getElementById("output");     
        
	if (output) {
	    while (output.hasChildNodes()) {
	        output.removeChild(output.lastChild);
	    }
	}

	if (result
		&& result.resourceSets
		&& result.resourceSets.length > 0
		&& result.resourceSets[0].resources
		&& result.resourceSets[0].resources.length > 0)
	{
	    if (output) {
	        var resultsListItem = null;
	        for (var j = 0; j < result.resourceSets[0].resources[0].routeLegs.length; j++) {

	            var resultsHeader = document.createElement("h5");
	            var resultsList = document.createElement("ol");
	            output.appendChild(resultsHeader);
	            output.appendChild(resultsList);

	            resultsHeader.innerHTML = "<br> Route from "
                    + result.resourceSets[0].resources[0].routeLegs[j].startLocation.name
                    + " to "
                    + result.resourceSets[0].resources[0].routeLegs[j].endLocation.name;
	            for (var i = 0; i < result.resourceSets[0].resources[0].routeLegs[j].itineraryItems.length; ++i) {
	                resultsListItem = document.createElement("li");
	                resultsList.appendChild(resultsListItem);
	                resultStr = result.resourceSets[0].resources[0].routeLegs[j].itineraryItems[i].instruction.text;
	                resultsListItem.innerHTML = resultStr;
	            }
	        }
	    }

		var bbox = result.resourceSets[0].resources[0].bbox;      
		var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(
			new Microsoft.Maps.Location(bbox[0], bbox[1]),
			new Microsoft.Maps.Location(bbox[2], bbox[3]));
		map.setView({ bounds: viewBoundaries});       
		var routeline = result.resourceSets[0].resources[0].routePath.line; 
		var routepoints = new Array();       
		for (var i = 0; i < routeline.coordinates.length; i++)       
		{            
			routepoints[i] = new Microsoft.Maps.Location(routeline.coordinates[i][0], routeline.coordinates[i][1]);
		}

		var pushpins = new Array();
		for (var i = 0; i < result.resourceSets[0].resources[0].routeLegs.length - 1; i++) {
		    var pushpinOptions = { icon: routeIcon, width: 30, height: 30 };
		    //var pushpinOptions = { anchor: new Microsoft.Maps.Point(10, 32) };
		    var leg = result.resourceSets[0].resources[0].routeLegs[i];
		    var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(leg.actualEnd.coordinates[0], leg.actualEnd.coordinates[1]), pushpinOptions);
            pushpins[i] = pushpin;
		}

		var routeshape = new Microsoft.Maps.Polyline(routepoints, {strokeColor:new Microsoft.Maps.Color(200,0,0,200)});          
		var startPushpinOptions = {anchor: new Microsoft.Maps.Point(10, 32) };      
		var startPin= new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(routeline.coordinates[0][0], routeline.coordinates[0][1]), startPushpinOptions);        
		//var endPushpinOptions = {anchor: new Microsoft.Maps.Point(10, 32)};         
		//var endPin= new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(routeline.coordinates[routeline.coordinates.length-1][0], routeline.coordinates[routeline.coordinates.length-1][1]), endPushpinOptions);      
		map.entities.push(startPin);      
		//map.entities.push(endPin);      
		map.entities.push(routeshape);
		for (var i = 0; i < pushpins.length; i++) {
		    map.entities.push(pushpins[i]);
		}
	}
	else
	{
		if (typeof(result.errorDetails) != 'undefined')         
		{
			resultsHeader.innerHTML = result.errorDetails[0];
		}
		alert("No Route found");
	}
}